package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Notice;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    @RequestMapping("/list")
    @RequiredPermission(name = "部门查询", expression = "notice:list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo) {
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        return "notice/list";
    }



    // 处理保存或者修改请求 /department/saveOrUpdate 参数在请求体
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {

        if (notice.getId() == null) {
            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }

        return "redirect:/notice/list";
    }

    @RequestMapping("/show")
    public String show(Model model, Long id , Integer haveRead) {

        if (id != null) {
            Notice notice = noticeService.get(id);
            model.addAttribute("notice", notice);
            noticeService.show(id, haveRead);
        }
        return "notice/show";
    }

    @RequestMapping("/updateStatus")
    public String updateStatus(Long id, Integer postStatus) {

        noticeService.updateStatus(id, postStatus);

        return "redirect:/notice/list"; // localhost/appointment/list
    }

    @RequestMapping("/delete")
    public String delete(Long id) {

        if (id != null) {
            noticeService.delete(id);
        }
        return "redirect:/notice/list"; // localhost/appointment/list
    }
}
